# To change this template, choose Tools | Templates
# and open the template in the editor.

__author__="root"
__date__ ="$Sep 23, 2009 10:25:27 PM$"

import re
import School
import xdrlib
from Grade import Grade

class StuClass:

    def __init__(self):
        self.teacher = ''
        self.teacher_email = ''
        self.period = ''
        self.class_name = ''
        self.grades = []

        self.MainClass = None



    def load_info(self, data ):
        self.Data = data

        periodmatch = re.findall(r"<td>(.+?)</td>", data, re.M)
        self.period = periodmatch[0]

        teachermatch = re.findall(r"<a href=\"mailto:(.+)\">([a-zA-Z, ]+)</a>", data, re.M)
        self.teacher_email = teachermatch[0][0]
        self.teacher = teachermatch[0][1]

        classnamematch = re.findall(r"<td align=\"left\">(.+?)<br>", data, re.M)
        self.class_name = classnamematch[0]

        for column in re.finditer("<a href=\"scores.html?(.+)fg=[a-zA-Z]+([0-9]+)\">(.+?)</a>", data):
            self.grades.append( Grade( column.group(2), column.group(3) ) )

        self.MainClass = School.GetClass(self.teacher, self.period, self.class_name)

    def CountActiveGrades(self):
        Count = 0

        for grade in self.grades:
            if grade.Active:
               Count = Count + 1

        return Count


    def WriteToBuffer(self, buff):

        if self.MainClass == None:
            return

        if self.CountActiveGrades() == 0:
            return

        buff.pack_uint( self.MainClass.UniqueId )
        buff.pack_uint( len( self.grades ) )

        for grade in self.grades:
            if grade.Active:
                grade.WriteToBuffer( buff )



    def __str__(self):
        RtnStr = 'Class: %s (%s) - %s' % (self.class_name, self.period, self.teacher)

        for grade in self.grades:
            if grade.Active:
                RtnStr += "\n\t" + str(grade)


        return RtnStr